package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/orderInfo")
public class ValidateCodeController {
    @Reference
    OrderService orderService;
    @Autowired
    JedisPool jedisPool;

    /**
     * 预约发送短信验证码,并将短信验证码缓存到redis中
     * @param tel   手机号码
     * @return      返回发送成功或者失败flag
     */
    @RequestMapping("/getVerificationCode")
    public Result getVerificationCode(@RequestParam("tel") String tel){
        try {
//            Integer code4String = ValidateCodeUtils.generateValidateCode(6);
//            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,tel,"123456");
            jedisPool.getResource().setex(tel+ RedisMessageConstant.SENDTYPE_ORDER,300,"123456");
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
    /**
     * 微信预约
     * @param map   前端返回参数 idCard,name,orderDate,setmealId,sex,telephone,validateCode
     * @return  预约结果flag
     */
    @RequestMapping("/weiXinOrder")
    public Result weiXinOrder(@RequestBody Map<String,Object> map){
        String validateCode = (String) map.get("validateCode");
        String telephone = (String) map.get("telephone");
        String validateCodeInRedis = jedisPool.getResource().get(telephone+RedisMessageConstant.SENDTYPE_ORDER);
        try {
            if (validateCode!=null&&telephone!=null&&validateCode.equals(validateCodeInRedis)){
                map.put("orderType", Order.ORDERTYPE_WEIXIN);
                return orderService.weiXinOrder(map);
            }
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /**
     * 登陆发送短信验证码,并将短信验证码缓存到redis中
     * @param tel 手机号码
     * @return    返回发送成功或者失败flag
     */
    @RequestMapping("/send4Login")
    public Result send4Login(@RequestParam("telephone") String tel){
        try {
            jedisPool.getResource().setex(tel+RedisMessageConstant.SENDTYPE_LOGIN,300,"123456");
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
}
